﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;

namespace MiniPac.Components
{
	[Serializable]
	public class HiScore
	{
		
		public SortedList<int, string> scores;

		public HiScore()
		{
			scores = new SortedList<int, string>(5);
		}

		public override String ToString()
		{

			StringBuilder sb = new StringBuilder();

			List<KeyValuePair<int, string>> lst = (scores.Reverse()).ToList();

			for (int i = 0; i < scores.Count; i++ )
			{
				sb.Append(lst[i].Key);
				sb.Append("\t\t");
				sb.Append(lst[i].Value);
				sb.Append("\n");
				if (i>=5)
				{
					break;
				}
			}
			return sb.ToString();
		}

		public void Save()
		{
			IFormatter serializer = new BinaryFormatter();
			FileStream saveFile = new FileStream("hiscore.bin", FileMode.OpenOrCreate, FileAccess.Write);
			serializer.Serialize(saveFile, scores);
			saveFile.Close();

		}

		public void Load()
		{
			IFormatter serializer = new BinaryFormatter();
			FileStream loadFile = new FileStream("hiscore.bin", FileMode.Open, FileAccess.Read);
			scores = serializer.Deserialize(loadFile) as SortedList<int, string>;
			loadFile.Close();
		}
	}
}
